#include "empire.h"

//empire-------------------------------
//constructor-----------
empire::empire(int pnum, string name){
    playerNum = pnum;
    empName = name;
}
empire::~empire(){
    shiping->clean();
    fleets->clean();
}
//design shiping--------
// call this after getting
// empire information.
// it'll help the player
// load/design its ships
//----------------------
void empire::designShiping(){
    //do this later
}
//access----------------
// return the player num
// or empire name or if
// the player has taken
// his turn
//----------------------
string empire::gName(){
    return empName;
}
int empire::gNum(){
    return playerNum;
}
bool empire::hasPlayed(){
    return played;
}
//take turn-------------
// displays the map on the
// mapScreen and dispatches
// orders
//----------------------
void empire::prepareTurn(){
    played = false;
}
void empire::beginTurn(){
}
void empire::docommand(command* cmd){
    cmd->preform();
}
void empire::endTurn(){
    played = true;
    turnNum++;
    //empireLib::getEmpireLib()->endTurn(playerNum);
    empireLib::releaseEmpireLib();
}
//=====================================
//empireLib++++++++++++++++++++++++++++
//constructor-----------
empireLib* empireLib::single;

empireLib* empireLib::getEmpireLib(){
    if(single != NULL && !single->inUse){
        single->inUse = true;
        return single;
    }
}
empireLib* empireLib::releaseEmpireLib(){
    single->inUse = false;
    return NULL;
}
bool empireLib::initialize(int playerNums){
    single = new empireLib(playerNums);
    return true;
}
empireLib::empireLib(int nump){
    inUse = false;
    players = new empire*[nump];
    pNum = nump;
}
empireLib::~empireLib(){
    for(int i = 0; i < pNum; i++){
        delete players[i];
    }
    delete players;
}
//orderPolicy-----------
// changes the order policy
// if necesary, then
// returns it
//----------------------
void empireLib::orderPolicy(tnOrder poll){
    orderPol = poll;
}
tnOrder empireLib::orderPolicy(){
    return orderPol;
}
//turnFunction----------
// these functions cycle
// through each players
// turns
// nextPlayer -
// returns the next player
// in line
// endTurn-
// called by the player
// when it finishes its
// turn - resets hasPlayed
// when all players have
// played
//----------------------
empire* empireLib::nextPlayer(){
    int next;
    switch(orderPol){
        case trnRandom:
            next = rand() % pNum;
            while(players[next]->hasPlayed()){
                next++;
                if(next >= pNum){
                    next = 0;
                }
            }
            return players[next];
            break;
        case trnList:
        case trnFixed:
            next = 0;
            while(players[next]->hasPlayed()){
                next++;
            }
            return players[next];
            break;
    }
}
void empireLib::endTurn(){
    numPlayed++;
    if(numPlayed > pNum){
        numPlayed = 0;
        currentTurn++;
        for(int i = 0; i < pNum; i++){
            players[i]->prepareTurn();
        }
    }
}
//=====================================

